package com.bigbird.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * 只能供一个客户端使用的网络程序
 * blocking io 阻塞式io
 * 这个accept 和 readline是阻塞的
 */
public class BioServerV1 {
    public static void main(String[] args) throws IOException {
        try(ServerSocket serverSocket = new ServerSocket()) {
            serverSocket.bind(new InetSocketAddress(1881));
            Socket accept = serverSocket.accept();
            accept.getOutputStream().write("hello world".getBytes(UTF_8));
            InputStream inputStream = accept.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
}
